<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="公司全称">
          <a-input placeholder="请选择公司" v-show v-decorator="['CompanyId', validatorRules.companyid]"></a-input>
          <a-input placeholder="请选择公司" @click="showCommonModal" v-decorator="['Company_CompanyName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择公司" v-decorator="['CompanyId', validatorRules.companyid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.CompanyId" v-for="(item,index) in companyData">{{item.CompanyName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="姓名">
          <a-input placeholder="请选择姓名" v-show v-decorator="['PersonalInfoId', validatorRules.personalinfoid]"></a-input>
          <a-input placeholder="请选择姓名" @click="showCommonUserModal" v-decorator="['PersonalInfo_PersonalName',{}]"></a-input>
          <!-- <a-select show-search :filter-option="filterOption" placeholder="请选择姓名" v-decorator="['PersonalInfoId', validatorRules.personalinfoid]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.PersonalInfoId" v-for="(item,index) in personData">{{item.PersonalName}}</a-select-option>
          </a-select> -->
        </a-form-item>
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="电话">
          <a-input :disabled="editType=='detail'" placeholder="请输入电话" v-decorator="['Tel', {}]" />
        </a-form-item> -->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="邮箱">
          <a-input :disabled="editType=='detail'" placeholder="请输入邮箱" v-decorator="['Email', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="职务名称">
          <a-input placeholder="请输入职务名称" v-decorator="['Position', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="职务标签">
          <a-select mode="tags" placeholder="请选择职务标签" v-decorator="['PositionTagId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.Value" v-for="(item,index) in PositionTagData">{{item.Value}}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="在职状态">
          <a-select placeholder="请选择在职状态" v-decorator="['WorkStatusId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.id" v-for="(item,index) in workStatusData">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
    <commonModal
      modalTitle="公司选择"
      ref="commonModal"
      @getCommonModalData="commonModalData"
      :columns="tableColumns"
      apiUrl="/companyInfo/queryRows"
      :searchArr="searchArr"></commonModal>
    <commonModal
      modalTitle="人员选择"
      rowKey="PersonalInfoId"
      ref="commonModalUser"
      @getCommonModalData="commonModalDataUser"
      :columns="tableColumnsUser"
      apiUrl="/personalInfo/queryRows"
      :searchArr="searchArrUser"></commonModal>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import commonModal from '@/components/commonModal'
  export default {
    name: "CompanyStaffModal",
    components:{
      commonModal
    },
    data () {
      return {
        tableColumnsUser:[
          { title: '姓名', dataIndex: 'PersonalName' },
          { title: '性别', dataIndex: 'Gender_Value' },
        ],
        searchArrUser:[
          { label: '姓名', key: 'PersonalName' },
        ],
        tableColumns:[
          { title: '公司全称', dataIndex: 'CompanyName' },
          { title: '公司编码', dataIndex: 'CompanyCode' },
        ],
        searchArr:[
          { label: '公司全称', key: 'CompanyName' },
          { label: '公司编码', key: 'CompanyCode' },
        ],
        companyData:[],
        personData:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        PositionTagData:[{
          id:'63',
          name:'高管'
        },{
          id:'64',
          name:'创始人/合伙人'
        },{
          id:'65',
          name:'行政'
        },{
          id:'66',
          name:'财务'
        },{
          id:'67',
          name:'法务'
        },{
          id:'68',
          name:'董秘/总助'
        },{
          id:'69',
          name:'HR'
        },{
          id:'70',
          name:'技术'
        },{
          id:'71',
          name:'产品'
        },{
          id:'72',
          name:'营销'
        },{
          id:'73',
          name:'运营'
        },{
          id:'74',
          name:'PR/GR'
        },{
          id:'75',
          name:'市场/品牌'
        },{
          id:'76',
          name:'政府项目'
        },{
          id:'77',
          name:'其它'
        }],
        workStatusData:[{
          id:'78',
          name:'在职'
        },{
          id:'79',
          name:'离职'
        }],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        companystaffid:{rules: [{ required: true, message: '请输入公司人员ID!' }]},
        companyid:{rules: [{ required: true, message: '请选择公司!' }]},
        personalinfoid:{rules: [{ required: true, message: '请输入个人信息Id!' }]},
        position:{rules: [{ required: true, message: '请输入职务名称!' }]},
        positiontagid:{rules: [{ required: true, message: '请输入职务标签!' }]},
        workstatusid:{rules: [{ required: true, message: '请输入在职状态!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        },
        url: {
          add: "/companyStaff/add",
          edit: "/companyStaff/update",
          baseList:"/baseInfo/queryRows",
        },
      }
    },
    created () {
      // this.getCompany();
      // this.getPerson();
      this.getBaseData();
    },
    methods: {
      showCommonUserModal () {
        this.$refs.commonModalUser.open()
      },
      showCommonModal () {
        this.$refs.commonModal.open()
      },
      commonModalData(val){
        this.form.setFieldsValue({
          CompanyId:val[0].CompanyId,
          Company_CompanyName:val[0].CompanyName
        })
      },
      commonModalDataUser(val){
        this.form.setFieldsValue({
          PersonalInfoId:val[0].PersonalInfoId,
          PersonalInfo_PersonalName:val[0].PersonalName
        })
      },
      getBaseData(){
        this.$http.postAction(this.url.baseList,{pageNo:1,pageSize:100,selectFields:[],conditions:[{
              "field":"TypeCode",
              "operator":"=",
              "value":'PositionTag'
            }]}).then((res)=>{
          if(res.status == 1){
            this.PositionTagData = res.data.rows;
          }
        })
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      getCompany(){
        this.$http.postAction('/companyInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
          if(res.status == 1){
            this.companyData = res.data.rows;
          }
        })
      },
      // getPerson(){
      //   this.$http.postAction('/personalInfo/queryRows',{pageNo:1,pageSize:9999}).then((res)=>{
      //     if(res.status == 1){
      //       this.personData = res.data.rows;
      //     }
      //   })
      // },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.model.PositionTagId = this.model.PositionTagId?this.model.PositionTagId.split(','):[];
        this.$nextTick(() => {
          this.model.WorkStatusId = this.model.WorkStatus;
          this.model.PersonalInfoId = this.model.PersonalInfo;
          this.model.CompanyId = this.model.Company;
          this.form.setFieldsValue(pick(this.model,'CompanyId','PersonalInfoId','Position','PositionTagId','WorkStatusId','Email','Tel','Company_CompanyName','PersonalInfo_PersonalName'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.CompanyStaffId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.PositionTagId = formData.PositionTagId?formData.PositionTagId.join():'';
            console.log(formData)
            this.$http.postAction(httpurl,formData,method).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>